﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace PressMenu2
{
    class GameObjectManager
    {
        List<GameObject> enemy;
        List<GameObject> mine;
        int i_next_diabay = 0;
        int nDiaBay = 30;
        double oldUpdate, newUpdate, oldFire;
        string msg;
        bool gameover = false;

        public void Render(SpriteBatch sb)
        {
            for (int i = 0; i < enemy.Count; i++)
                if(enemy[i] != null)
                    enemy[i].Render(sb);

            for (int i = 0; i < mine.Count; i++)
                if (mine[i] != null)
                    mine[i].Render(sb);
            
            string s = enemy.Count.ToString() +"/" + nDiaBay.ToString();
            sb.DrawString(MyResource.fontArial25,s,new Vector2(0,0),Color.Yellow);
            sb.DrawString(MyResource.fontArial25, msg, new Vector2(cDef.WindowWidth/2, cDef.WindowHeight/2), Color.Red);
        }

        public int Update(GameTime gameTime,MyInput input)
        {
            if (gameover)
                return -1;

            if (input.Mouse.IsLeftClick())
                Fire();

            newUpdate = gameTime.TotalGameTime.TotalMilliseconds;
            for (int i = 0; i < enemy.Count; i++)
                if (enemy[i] != null)
                {
                    int re = enemy[i].Update(gameTime, input);
                    DispathUpdateCode(enemy,re,i);
                }

            for (int i = 0; i < mine.Count; i++)
                if (mine[i] != null)
                {
                    int re = mine[i].Update(gameTime, input);
                    DispathUpdateCode(mine,re, i);

                }

            NewUpdate();
            CheckCollision();
            CheckGameOver();
            
            return 1;
        }

        private void CheckGameOver()
        {
            msg = "";
            gameover = false;

            if (enemy.Count == 0)
                msg = "YOU ARE WIN!";
            else
                if (mine[0].Blood <= 0)
                {
                    msg = "GAME OVER";
                    gameover = true;
                }
        }

        private void DispathUpdateCode(List<GameObject> listObject, int re, int index)
        {
            switch (re)
            {
                case -1:        // hủy
                    listObject.RemoveAt(index);
                    break;
                case 0:
                    break;
                case 1:
                    break;
                case 2: mine[0].Blood -= listObject[index].Power;
                    break;
            }
        }

        private void Fire()
        {
            if (((CungTen)mine[0]).Free == false)
            {
                oldFire = newUpdate;
                ((CungTen)mine[0]).Free = true;
            }
        }

        private bool CheckCollision()
        {
            for (int i = 0; i < mine.Count; i++)
                for (int j = 0; j < enemy.Count; j++)
                {
                    if (mine[i].CheckCollision(enemy[j]))
                    {
                        mine[i].Collision(enemy[j].Power);
                        enemy[j].Collision(mine[i].Power);
                    }
                }
            return false;
        }

        public GameObjectManager()
        {
            enemy = new List<GameObject>(); 
            mine = new List<GameObject>();
            mine.Add(new CungTen(0, 0));
        }

        public void NewUpdate()
        {
            double distance = newUpdate - oldUpdate;
            if (distance > 2000) // sau 2000ms tạo ra 1 đãi bay, tối đa nDiaBay
            {
                if (i_next_diabay++ < nDiaBay)
                    enemy.Add(new DiaBay(0, 1));
                oldUpdate = newUpdate;
            }

            distance = newUpdate - oldFire;
            if (distance > 1000) // 1000 = thời gian ngắt quãng giữa 2 lần bắn liên tiếp
                if(((CungTen)mine[0]).Free)
                {
                    mine.Add(new MuiTen(0, 0));
                    ((CungTen)mine[0]).Free = false;
                }
        }
    }
}
